package com.sip.stream.utils.zfy.consumer;

import android.media.MediaFormat;
import android.util.Log;

import com.sip.stream.utils.zfy.GSMediaSampleData;
import com.sip.stream.utils.zfy.encoder.IGSVideoEncoder;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import kotlin.jvm.internal.ByteCompanionObject;

public class DSJGroupintercomConsumer implements IGSPlatformConsumer {
    private static final String TAG = "DSJGroupintercomConsumer";

    private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    private byte[] SPSdata;
    private AtomicInteger fpsCounter;
    private Disposable fpsCounterDisposable;
    private long startTimestampUs;
    private boolean stopFlag;
    private MediaFormat vdieoFormat;
    private IGSMediaObserver<IGSVideoEncoderConsumer> videoEncoderConsumerObserver;
    private IGSVideoEncoderConsumer videoEncoderLisener;
    private IGSMediaObserver<IGSWAVConsumer> wavConsumerObserver;

    @Override
    public void forceSwitchMuxer() {
    }

    @Override
    public void onAudioStream(byte[] bArr, long j) {
    }

    @Override
    public void start() {
    }

    public DSJGroupintercomConsumer() {
        this.fpsCounter = new AtomicInteger();
        this.startTimestampUs = 0L;
        this.videoEncoderLisener = new IGSVideoEncoderConsumer() {
            @Override
            public void encodeVideoError(String str) {
            }

            @Override
            public void encodeVideoSuccess(GSMediaSampleData gSMediaSampleData) {
                DSJGroupintercomConsumer.this.encodeVideoSuccess(gSMediaSampleData);
            }

            @Override
            public void setOutputFormat(MediaFormat mediaFormat) {
                DSJGroupintercomConsumer.this.vdieoFormat = mediaFormat;
            }

            @Override
            public void addObserver(IGSMediaObserver<IGSVideoEncoderConsumer> iGSMediaObserver) {
                DSJGroupintercomConsumer.this.videoEncoderConsumerObserver = iGSMediaObserver;
            }
        };
    }

    public DSJGroupintercomConsumer(IGSVideoEncoder iGSVideoEncoder) {
        this.fpsCounter = new AtomicInteger();
        this.startTimestampUs = 0L;
        IGSVideoEncoderConsumer iGSVideoEncoderConsumer = new IGSVideoEncoderConsumer() {
            @Override
            public void encodeVideoError(String str) {
            }

            @Override
            public void encodeVideoSuccess(GSMediaSampleData gSMediaSampleData) {
                DSJGroupintercomConsumer.this.encodeVideoSuccess(gSMediaSampleData);
            }

            @Override
            public void setOutputFormat(MediaFormat mediaFormat) {
                DSJGroupintercomConsumer.this.vdieoFormat = mediaFormat;
            }

            @Override
            public void addObserver(IGSMediaObserver<IGSVideoEncoderConsumer> iGSMediaObserver) {
                DSJGroupintercomConsumer.this.videoEncoderConsumerObserver = iGSMediaObserver;
            }
        };
        this.videoEncoderLisener = iGSVideoEncoderConsumer;
        iGSVideoEncoder.addLisener(iGSVideoEncoderConsumer);
    }

    @Override
    public void stop() {
        Log.i(TAG, "stop");
        this.stopFlag = true;
        IGSMediaObserver<IGSVideoEncoderConsumer> iGSMediaObserver = this.videoEncoderConsumerObserver;
        if (iGSMediaObserver != null) {
            iGSMediaObserver.onStop(this.videoEncoderLisener);
            this.videoEncoderConsumerObserver = null;
        }
        IGSMediaObserver<IGSWAVConsumer> iGSMediaObserver2 = this.wavConsumerObserver;
        if (iGSMediaObserver2 != null) {
            iGSMediaObserver2.onStop(this);
            this.wavConsumerObserver = null;
        }
    }

    @Override
    public void stopVideo() {
        if (this.videoEncoderConsumerObserver != null) {
            this.videoEncoderConsumerObserver.onStop(this.videoEncoderLisener);
            this.videoEncoderConsumerObserver = null;
        }
    }

    @Override
    public void addObserver(IGSMediaObserver<IGSWAVConsumer> iGSMediaObserver) {
        this.wavConsumerObserver = iGSMediaObserver;
    }

    private void startFPSCounter() {
        Observable.interval(1L, TimeUnit.SECONDS).subscribe(new Observer<Long>() {
            @Override
            public void onComplete() {
            }

            @Override
            public void onError(Throwable th) {
            }

            @Override
            public void onSubscribe(Disposable disposable) {
                DSJGroupintercomConsumer.this.fpsCounterDisposable = disposable;
            }

            @Override
            public void onNext(Long l) {
                DSJGroupintercomConsumer.this.fpsCounter.set(0);
            }
        });
    }

    private void stopFPSCounter() {
        if (this.fpsCounterDisposable != null) {
            this.fpsCounterDisposable.dispose();
            this.fpsCounterDisposable = null;
        }
    }

    public void encodeVideoSuccess(GSMediaSampleData gSMediaSampleData) {
        if (gSMediaSampleData.getBufferInfo().flags == 1) {
            if (this.SPSdata != null) {
                sendSPS_PPS(gSMediaSampleData);
            }
        } else if (gSMediaSampleData.getBufferInfo().flags == 2) {
            ByteBuffer byteBuf = gSMediaSampleData.getByteBuf();
            int remaining = byteBuf.remaining();
            byte[] bArr = new byte[remaining];
            byteBuf.get(bArr);
            byte[] bArr2 = new byte[remaining];
            this.SPSdata = bArr2;
            System.arraycopy(bArr, 0, bArr2, 0, remaining);
        }
    }

    private void printInfo(ByteBuffer byteBuffer) {
        Log.i(TAG, "encodeVideoSuccess remaining" + byteBuffer.remaining() + " position" + byteBuffer.position() + " limit" + byteBuffer.limit() + " capacity" + byteBuffer.capacity() + (byteBuffer.isDirect() ? " Direct" : " Not Direct"));
    }

    private void sendSPS_PPS(GSMediaSampleData gSMediaSampleData) {
        byte[] bArr = this.SPSdata;
        if (bArr == null) {
            return;
        }
        ByteBuffer allocateDirect = ByteBuffer.allocateDirect(bArr.length + gSMediaSampleData.getByteBuf().remaining());
        allocateDirect.put(this.SPSdata);
        allocateDirect.put(gSMediaSampleData.getByteBuf());
        allocateDirect.position(0);
    }

    @Override
    public void setVideoEncoder(IGSVideoEncoder iGSVideoEncoder) {
        iGSVideoEncoder.addLisener(this.videoEncoderLisener);
        iGSVideoEncoder.startEncode();
    }

    private synchronized long getPTS(long j, boolean z) {
        return j / 1000;
    }

    private byte[] createSPSdata() {
        return new byte[]{0, 0, 0, 1, 103, 100, 0, 31, -84, -76, 2, ByteCompanionObject.MIN_VALUE, 45, -46, -112, 80, 32, 32, 109, 10, 19, 80, 0, 0, 0, 1, 104, -18, 6, -30, -64};
    }

    private static String bytes2HexString(byte[] bArr) {
        char[] cArr = new char[bArr.length << 1];
        int i = 0;
        for (byte b : bArr) {
            int i2 = i + 1;
            char[] cArr2 = HEX_DIGITS;
            cArr[i] = cArr2[(b >> 4) & 15];
            i = i2 + 1;
            cArr[i2] = cArr2[b & 15];
        }
        return new String(cArr);
    }

    public boolean isStop() {
        return this.stopFlag;
    }
}
